var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData: true,
    pageIndex:1,
    totalNumber:"",
    size:11,
    listCategoryOne: '',
    categorySelect: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.pageScrollTo({ scrollTop: 0, duration: 0 })
    this.setData({
      pageIndex: 1
    })
    this.myquary()
  },
  //增加风险设置
  goAddRiskSetting: function () {
    wx.navigateTo({
      url: 'riskSetting_add',
    })
  },
  //查询风险设置
  myquary:function(){
    var that=this;
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    })
    wx.request({
      method: "POST",
      url: app.preUrl + 'getRisk',//查询风险列表
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        userId: app.globalData.userId,
        size: that.data.size,
        page: that.data.pageIndex
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading();
        if (res.data.code == 100) {
          var arr0 = res.data.result
          if (that.data.pageIndex != 1) {
            var arr1 = that.data.listCategoryOne
            arr1 = arr1.concat(arr0)
            that.setData({ listCategoryOne: arr1 })
          } else {
            that.setData({ listCategoryOne: arr0 })
          }
          that.setData({
            totalNumber: res.data.total,
            pageIndex: res.data.page,
            ifShownoData: true
          });
        } else {
          that.setData({ ifShownoData: false });
        }
        
      },
      fail: function (error) {
        that.setData({ buthiddenNext: true });
      }
    })
  },
  // 删除货名
  deleteImg: function (e) {
    var that = this;
      wx.showModal({
        title: '确认操作',
        content: '确认删除该类目 ',
        confirmText: "确认",
        confirmColor: "#56BDE0",
        success(res) {
          if (res.confirm) {
            wx.request({
              method: "POST",
              url: app.preUrl + 'deleteRisk',//删除风险设置
              dataType: 'json',
              header: { 
                'content-type': 'application/x-www-form-urlencoded',
              },
              data: {
                userId: app.globalData.userId,
                parentId: e.currentTarget.dataset.parentid,
                categoryId: e.currentTarget.dataset.select
              },
              success: function (res) {
                console.log(res.data.code)
                if (res.data.code==100){
                  wx.pageScrollTo({ scrollTop: 0, duration: 0 })
                  that.setData({
                    pageIndex: 1
                  })
                  that.myquary()
                }
               
              },
              fail: function (error) {
                that.setData({ buthiddenNext: true });
              }
            })
          } else if (res.cancel) {
            //console.log('用户点击取消')
          }
        }
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.pageScrollTo({ scrollTop: 0, duration: 0 })
    this.setData({
      pageIndex: 1
    })
    this.myquary()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that=this
    that.setData({ pageIndex: 1 });
    this.myquary();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    var pageIndex = that.data.pageIndex
    if (pageIndex < that.data.totalNumber / that.data.size) {
      pageIndex++
      that.setData({ pageIndex: pageIndex });
      this.myquary()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})